package com.example.isoftservice.controller;

import com.example.isoftservice.mapper.MessageMapper;
import com.example.isoftservice.pojos.Message;
import com.example.isoftservice.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageMapper messageMapper;
//查询操作列表
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result<Map<String ,Object>> list(@RequestParam Integer pageNo,Integer pageSize) {
        Result<Map<String,Object>> result = new Result<>();
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNo,pageSize);
        List<Map<String,Object>> resultList = messageMapper.list();
        PageInfo<Map<String,Object>> info = new PageInfo<>(resultList);
        map.put("total",info.getTotal());
        map.put("rows",info.getList());
        result.setCode(0);
        result.setResult(map);
        return  result;
    }

    @RequestMapping(value = "/removeByIds",method = RequestMethod.POST)
    public Result<Boolean> removeByIds(@RequestBody Message message) {
        Result<Boolean> result = new Result<>();
        Boolean flag = messageMapper.deleteByIds(message.getIds()) >= 1 ? true : false;
        if (flag) {
            result.setCode(0);
            result.setMessage("删除成功!");
            return result;
        } else {
            result.setCode(1);
            result.setMessage("删除失败");
            return result;
        }
    }
}
